package com.cloudlead.userAction.Action;

import com.cloudlead.common.lang.annotation.Label;
import com.cloudlead.common.lang.persistence.EasyuiGridData;
import com.cloudlead.common.lang.persistence.ObjectMap;
import com.cloudlead.common.lang.persistence.Page;
import com.cloudlead.common.worker.action.impl.AbstractActionFactory;
import com.cloudlead.common.worker.annotation.Action;
import com.cloudlead.common.worker.security.annotation.RequireGuest;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseCompanyUser;
import com.cloudlead.userAction.BusinessModules;
import com.cloudlead.userAction.Service.UserRegisterService;
import com.cloudlead.userService.UserManagement;
import com.cloudlead.userService.vo.UserManagement1VO;
import com.cloudlead.userService.vo.UserManagementVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.validation.constraints.NotNull;

/**
 * Created by Administrator on 2015-08-13.
 */
@Controller
public class UserRegisterAction extends AbstractActionFactory implements UserManagement {

    @Autowired
    UserRegisterService userRegister;

    public UserRegisterAction() {

        super(BusinessModules.UserRegiter);
    }

    @Action
    @Label("用户注册")
    @Override
    @RequireGuest//取消token验证
    /**
     * 将web页面传过来的userManagementVO对象的值copy给baseCompanyUser。
     * 由于baseCompanyId是BaseCompanyUser中的外键，所以不能完全copy。
     */
    public String userRegister(UserManagementVO vo) {
        BaseCompanyUser baseCompanyUser=new BaseCompanyUser();
        BeanUtils.copyProperties(vo,baseCompanyUser);
        BaseCompany baseCompany=new BaseCompany();
        baseCompany.setId(vo.getBaseCompanyId());
        baseCompanyUser.setBaseCompany(baseCompany);

        userRegister.userRegister(baseCompanyUser);

        return "用户注册成功！";
    }

    @Action
    @Label("用户编辑")
    @RequireGuest//取消token验证
    @Override
    public void editUser(UserManagementVO vo) {
        userRegister.editUser(vo);
    }

    @Action
    @Label("用户查询（单值）")
    @Override
    public ObjectMap findOneUser(UserManagement1VO userManagement1VO) {
        return  new ObjectMap(userRegister.findOneUser(userManagement1VO),

                "id," +
                        "baseCompany.companyName companyName," +
                        "baseCompany.id companyID," +
                        "baseCompany.identity companyIdentity,"+
                        "baseCompany.logoImage logoImage," +"baseCompany.companyName companyName" );
    }

    @Action
    @Label("用户查询（分页）")
    @Override
    public EasyuiGridData findAllUser(UserManagement1VO userManagement1VO,
                                              @Label("每页行数")int rows, @Label("页码")int page) {
        return new EasyuiGridData(userRegister.finAllUser(userManagement1VO, rows, page),
                "id,isAdmin,userName,phone,eMail,name,weixinNamee,password,status,baseCompany.id baseCompanyId,baseCompany.identity identity");
    }

    @Action
    @Label("用户删除")
    @Override
    public void delUser(@Label("ID")String id) {
        userRegister.delUser(id);
    }

    @Action
    @Label("用户更新权限")
    @Override
    public String updatePrivilege(@Label("用户名")@NotNull String userName, @Label("权限列表")@NotNull String privilegeList) {
        return  userRegister.addPrivilege(userName, privilegeList);
    }
    @Action
    @Label("修改密码")
    @Override
    public void editPass(@Label("用户编号")@NotNull String userID, @Label("旧密码")@NotNull String password, @Label("新密码")@NotNull String editPass) {
        userRegister.editPass(userID,password,editPass);
    }

/*    @Action
    @Label("用户删除权限")
    @Override
    public String delPrivilege(@Label("用户ID")@NotNull String userId) {
        return userRegister.delPrivilege(userId);
    }*/
}
